package com.hp.docker_base.controller;

import com.hp.docker_base.bean.AnoreFidOutDto;
import com.hp.docker_base.bean.ThroatFidOutDto;
import com.hp.docker_base.bean.Department;
import com.hp.docker_base.bean.StomachFidOutDto;
import com.hp.docker_base.bean.em.EnumDepartment;
import com.hp.docker_base.bean.em.EnumOKOrNG;
import com.hp.docker_base.service.IDepartmentService;
import com.hp.docker_base.service.impl.AnorectalService;
import com.hp.docker_base.service.impl.StomachService;
import com.hp.docker_base.service.impl.ThroatService;
import com.hp.docker_base.util.CommonUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/smart-medical")
public class SmartMedicalController {


    @Autowired
    private ThroatService throatService;

    @Autowired
    private StomachService stomachService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    AnorectalService anorectalService;

    //请求参数
    // http://localhost:8083/smart-medical/diagnostic/result
    @ApiOperation(value = "获取诊断结果", notes = "获取诊断结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "RecId", value = "诊断编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "DeptId", value = "科室编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "DataIn", value = "[{FidIn:\"输入特征Id1\", Value:\"特征数值\"}, \n" +
                    "               {FidIn:\"输入特征Id2\", Value:\"特征数值\"}, \n" +
                    "               {FidIn:\"输入特征Id3\", Value:\"特征数值\"}", paramType = "query", required = true)
    })
    @RequestMapping("/diagnostic/result")
    public Map<String, Object> doQueryDiagnosticFeatureList(
            @RequestParam(value = "RecId") String RecId,
            @RequestParam(value = "DeptId") String DeptId,
            @RequestParam(value = "DataIn") String DataIn) throws IOException {

        Department department = departmentService.queryDepartmentByUUID(DeptId);
        if(department == null){
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(), null);
        }

        if(EnumDepartment.LARYNGOLOGY.getCode().equals(department.getUuid())){
            // 咽喉
            ThroatFidOutDto throatFidOutDto = throatService.getMedicalResult(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),throatFidOutDto);
        }else if(EnumDepartment.DIGESTIVE.getCode().equals(department.getUuid())){
            // 消化
            StomachFidOutDto stomachFidOutDto = stomachService.CalculateRes(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(), stomachFidOutDto);
        }else if(EnumDepartment.Anorectal1.getCode().equals(department.getUuid())){
            //肛肠科-感染性与非感染性
            AnoreFidOutDto anoreFidOutDto = anorectalService.CalculateRes1(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),anoreFidOutDto);
        }else if(EnumDepartment.Anorectal21.getCode().equals(department.getUuid())){
            //肛肠科-慢性细菌痢疾和肠结核
            AnoreFidOutDto anoreFidOutDto = anorectalService.CalculateRes21(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),anoreFidOutDto);
        }else if(EnumDepartment.Anorectal22.getCode().equals(department.getUuid())){
            //肛肠科-功能性肠病和器质性肠病
            AnoreFidOutDto anoreFidOutDto = anorectalService.CalculateRes22(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),anoreFidOutDto);
        } else if(EnumDepartment.Anorectal31.getCode().equals(department.getUuid())){
            //肛肠科-肠易激肠病的种类诊断
            AnoreFidOutDto anoreFidOutDto = anorectalService.CalculateRes31(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),anoreFidOutDto);
        }else if(EnumDepartment.Anorectal32.getCode().equals(department.getUuid())){
            //肛肠科-器质性肠病的种类诊断
            AnoreFidOutDto anoreFidOutDto = anorectalService.CalculateRes32(RecId, DeptId, DataIn);
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(),anoreFidOutDto);
        } else{
            return CommonUtil.setReturnMap(EnumOKOrNG.OK.getCode(), EnumOKOrNG.OK.getValue(), null);
        }
    }


    @RequestMapping("/updateRule")
    public List<String> updateRule(
            @RequestParam(value = "RecId") String RecId,
            @RequestParam(value = "DeptId") String DeptId,
            @RequestParam(value = "ThroatRule") String newRule) {

        Department department = departmentService.queryDepartmentByUUID(DeptId);
        if(department == null){
            return null;
        }

        if(EnumDepartment.LARYNGOLOGY.getCode().equals(department.getUuid())){
            // 咽喉
            return throatService.updateService(newRule);
        }else if(EnumDepartment.DIGESTIVE.getCode().equals(department.getUuid())){
            // 消化
            return stomachService.updateService(newRule);
        }else{
            return null;
        }
    }


    @RequestMapping("/showRules")
    public List<Map<String, Object>> showRules(
            @RequestParam(value = "RecId") String RecId,
            @RequestParam(value = "DeptId") String DeptId) {

        Department department = departmentService.queryDepartmentByUUID(DeptId);
        if(department == null){
            return null;
        }

        if(EnumDepartment.LARYNGOLOGY.getCode().equals(department.getUuid())){
            // 咽喉
            return throatService.showRules();
        }else if(EnumDepartment.DIGESTIVE.getCode().equals(department.getUuid())){
            // 消化
            return stomachService.showRules();
        }else{
            return null;
        }
    }

    @RequestMapping("/deleteRule/{id}")
    public String delete(
            @RequestParam(value = "RecId") String RecId,
            @RequestParam(value = "DeptId") String DeptId,
            @PathVariable int id) {

        Department department = departmentService.queryDepartmentByUUID(DeptId);
        if(department == null){
            return null;
        }

        if(EnumDepartment.LARYNGOLOGY.getCode().equals(department.getUuid())){
            // 咽喉
            throatService.deleteRule(id);
            return "已删除第" + id + "条规则";
        }else if(EnumDepartment.DIGESTIVE.getCode().equals(department.getUuid())){
            // 消化
            return stomachService.deleteRule(id);
        }else{
            return null;
        }
    }

}
